package com.yh.csx.bsf.ods;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;


@Component
public class ScheduleReame implements SchedulingConfigurer {

	private static Logger logger = LoggerFactory.getLogger(ScheduleReame.class);

	@Autowired
	private RenameTask task;
		
	
	@Autowired
	ThreadPoolTaskExecutor taskExecutor;
	private volatile ScheduledTaskRegistrar registrar;
	private final ConcurrentHashMap<String, ScheduledFuture<?>> scheduledFutures = new ConcurrentHashMap<>();
	private final ConcurrentHashMap<String, CronTask> cronTasks = new ConcurrentHashMap<>();

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		this.registrar = taskRegistrar;
		// 每5s扫描一次任务列表
		this.registrar.addTriggerTask(() -> {
			this.refreshTasks();
		}, triggerContext -> new PeriodicTrigger(5L, TimeUnit.SECONDS).nextExecutionTime(triggerContext));
	}

	/**
	 * 刷新任务
	 */
	private void refreshTasks() {
		
		Set<String> taskIds = scheduledFutures.keySet();
		for (String taskId : taskIds) {
			
		}		
	}

	@PreDestroy
	public void destroy() {
		this.registrar.destroy();
	}
	
	@Component
	@Configuration
	@ConfigurationProperties(prefix="strategy.rename")
	public static class RenameTask{
		
		private Map<String, Mapping> tables;

		public Map<String, Mapping> getTables() {
			return tables;
		}

		public void setTables(Map<String, Mapping> tables) {
			this.tables = tables;
		}
	}
	
	public static class Mapping{
		private String schedule;
		private String table;
		
		public String getSchedule() {
			return schedule;
		}
		public void setSchedule(String schedule) {
			this.schedule = schedule;
		}
		public String getTable() {
			return table;
		}
		public void setTable(String table) {
			this.table = table;
		}
		
		
	}
	

}


